<?php
/*************************************************
首页主控制器 - the
Author: Reload <17-10-20>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index_old(){
        // print_r($_SESSION);
        $map['is_hot'] = 0;
        $map['status'] = 0;
        $platform = D('Platform')->get_all_page($map,0,9999,$order='`order` asc');
        $map = array();
        foreach($platform as $k => $v) {
            $map['platform_id'] = $v['id'];
            if($v['user_roi']) {
                $platform[$k]['sch_user_roi'] = $v['start_amount']*$v['user_roi']/100;
            }
            $plat_sch_count = D('PlatformObject')->count($map);
            $platform[$k]['plat_sch_count'] = $plat_sch_count;
        }

        $map['is_hot'] =1;
		$map['status'] = 0;
        $platform_jx = D('Platform')->get_all_page($map,0,99999,$order='`order` asc');
        $map = array();
        foreach($platform_jx as $k => $v) {
            $map['platform_id'] = $v['id'];
            if($v['user_roi']) {
                $platform_jx[$k]['sch_user_roi'] = $v['start_amount']*$v['user_roi']/100;
            }
            $plat_sch_count = D('PlatformObject')->count($map);
            $platform_jx[$k]['plat_sch_count'] = $plat_sch_count;
        }

        //顶部banner广告
        $wx_banner_index = R('Ad/get_ad',array('tag'=>'wx_banner_index'));
        // print_r($wx_banner_index);
        //中部banner广告
        $wx_middle_index = R('Ad/get_ad',array('tag'=>'wx_middle_index'));
        //print_r($wx_middle_index);
        $this->assign('wx_banner_index',$wx_banner_index);
        $this->assign('wx_middle_index',$wx_middle_index);
        $this->assign('platform',$platform);
        $this->assign('platform_jx',$platform_jx);
        $this->display();
    }

	 public function index(){
        //个人信息
        $user_id = $_SESSION['user']['id'];
        $map['id'] = $user_id;
        $user = D('User')->get_one($map);
        if($user){
            //根据身份证号，自动返回性别  
            $sexint = (int)substr($user['idcard'],16,1);  
            if($sexint % 2 === 0){
                $user['sex']="女士";
            }else{
                $user['sex']="先生";

            }  

            if($user['realname']){
                $user['firstname'] =mb_substr($user['realname'],0,1,'utf8').$user['sex'];  
            }
            
        }
        
          //计算当前时间
        $now = time();
        $today = date('H',time());
        if($today>=6 and $today<=12){
            $now_time = "早上";
        }else if($today>12 and $today<=14){
            $now_time = "中午";
        }else if($today>14 and $today<18){
            $now_time = "下午";
        }else{
            $now_time = "晚上";
        }

        //广告
        $wx_banner_index = R('Ad/get_ad',array('tag'=>'wx_banner_index'));
        $m_hot = R('Ad/get_ad',array('tag'=>'m_hot'));
        //网贷早报
        $today_video = R('Push/get_push',array('tag'=>'m_mon_video'));
        foreach($today_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $today_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
           // print_r($video_info);
            $today_video[$k]['content_short'] =$news['content_short'];
			$today_video[$k]['content_short'] =$news['content_short'];
            $today_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
		}
       // 理财学堂
       $school_video = R('Push/get_push',array('tag'=>'m_school'));
        foreach($school_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $school_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
           // print_r($video_info);
            $school_video[$k]['content_short'] =$news['content_short'];
			$school_video[$k]['content_short'] =$news['content_short'];
            $school_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
        }
         // 理财学堂
       $wdfy_video = R('Push/get_push',array('tag'=>'m_wdfy'));
        foreach($wdfy_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $wdfy_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $wdfy_video[$k]['content_short'] =$news['content_short'];
			$wdfy_video[$k]['content_short'] =$news['content_short'];
            $wdfy_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play']; 
        }
		$ptzf_video = R('Push/get_push',array('tag'=>'m_ptzf'));
        foreach($ptzf_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $ptzf_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $ptzf_video[$k]['content_short'] =$news['content_short'];
            $ptzf_video[$k]['content_short'] =$news['content_short'];
            $ptzf_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
        }

        $rw_video = R('Push/get_push',array('tag'=>'m_rw'));
        foreach($rw_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $rw_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $rw_video[$k]['content_short'] =$news['content_short'];
            $rw_video[$k]['content_short'] =$news['content_short'];
            $rw_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
        }


         //投顾
        if($user){
            $map = array();
            $map['user_id'] = $user['referral'];
            $recom = D('Recommend')->get_one($map);
        }
        $map = array();
        $map['is_recom'] = 1;
        $recommend_list = D('Recommend')->get_all($map,$order="add_time asc");
        foreach ($recommend_list as $k => $v) {
            $recommend_list[$k]['user_info'] = $user['nickname'];
            $user_id = $v['user_id'];
            $map = array();
            $map['id'] = $user_id;
            $userinfo = D('User')->get_one($map);
            $recommend_list[$k]['nickname'] = $userinfo['nickname'];
            $recommend_list[$k]['url_img'] = $userinfo['url_img'];
            if($v['id']==$recom['id']){
                $recommend_list[$k]['is_fans'] = 1;
            }else{
                $recommend_list[$k]['is_fans'] = 0;
            }
        }
        $this->assign('rw_video',$rw_video);
        $this->assign('ptzf_video',$ptzf_video);
        $this->assign('recommend_list',$recommend_list);
        $this->assign('now_time',$now_time);
        $this->assign('user',$user);
        $this->assign('wdfy_video',$wdfy_video);
        $this->assign('school_video',$school_video);
        $this->assign('today_video',$today_video);
        $this->assign('wx_banner_index',$wx_banner_index);
        $this->assign('m_hot',$m_hot);
        $this->display();
    }
    //验证是否推手
    public function check_push() {
        $user_id = I('post.user_id');
        $map['user_id'] = $user_id;
        $recom = D('Recommend')->get_one($map);
        if($recom) {
            $data = array('err'=>0);
        } else {
            $data = array('err'=>1);
        }
        $this->ajaxReturn($data);
    }
    //app下载
    public function download(){
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { 
            $this->display('Index/w_page');
        }else{
             header('Location:http://www.8hjie.com/Public/Android/8hjie_v1.0.1.apk');
        }
    }
	
	public function popularize(){
		$this->display();
	}

}